package com.atguigu.encryption.utils;
import org.bouncycastle.crypto.digests.SM3Digest;

public class SM3Util {

    public static byte[] hash(String input) {
        byte[] data = input.getBytes();
        SM3Digest digest = new SM3Digest();
        digest.update(data, 0, data.length);
        byte[] hashValue = new byte[digest.getDigestSize()];
        digest.doFinal(hashValue, 0);
        return hashValue;
    }

    public static void main(String[] args) {
        // 要进行哈希的数据
        String input = "Hello, SM3!";

        // 计算SM3哈希值
        byte[] hashValue = hash(input);

        // 打印哈希值
        System.out.println("SM3 Hash: " + bytesToHex(hashValue));
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder(2 * bytes.length);
        for (int i = 0; i < bytes.length; i++) {
            hexString.append(Integer.toHexString(0xFF & bytes[i]));
        }
        return hexString.toString();
    }
}
